home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / mimelib / datetime.h < prev    next >
Encoding:
C/C++ Source or Header  |  2007-05-14  |  11.1 KB  |  351 lines

  1. //=============================================================================
  2. // File:       datetime.h
  3. // Contents:   Declarations for DwDateTime
  4. // Maintainer: Doug Sauder <dwsauder@fwb.gulf.net>
  5. // WWW:        http://www.fwb.gulf.net/~dwsauder/mimepp.html
  6. //
  7. // Copyright (c) 1996, 1997 Douglas W. Sauder
  8. // All rights reserved.
  9. //
  10. // IN NO EVENT SHALL DOUGLAS W. SAUDER BE LIABLE TO ANY PARTY FOR DIRECT,
  11. // INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
  12. // THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF DOUGLAS W. SAUDER
  13. // HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. //
  15. // DOUGLAS W. SAUDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT
  16. // NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  17. // PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"
  18. // BASIS, AND DOUGLAS W. SAUDER HAS NO OBLIGATION TO PROVIDE MAINTENANCE,
  19. // SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  20. //
  21. //=============================================================================
  22.  
  23. #ifndef DW_DATETIME_H
  24. #define DW_DATETIME_H
  25.  
  26. #include <time.h>
  27.  
  28. #ifndef DW_CONFIG_H
  29. #include <mimelib/config.h>
  30. #endif
  31.  
  32. #ifndef DW_FIELDBDY_H
  33. #include <mimelib/fieldbdy.h>
  34. #endif
  35.  
  36. //=============================================================================
  37. //+ Name DwDateTime -- Class representing an RFC-822 date-time
  38. //+ Description
  39. //. {\tt DwDatetime} represents a {\it date-time} as described in RFC-822
  40. //. and RFC-1123.  The parse method for {\tt DwDateTime} parses the
  41. //. string representation to extract the year, month, day, hour, minute,
  42. //. second, and time zone.  {\tt DwDateTime} provides member functions
  43. //. to set or get the individual components of the date-time.
  44. //=============================================================================
  45. // Last modified 1997-08-23
  46. //+ Noentry ~DwDateTime mYear mMonth mDay mHour mMinute mSecond mZone
  47. //+ Noentry sDefaultZone sIsDefaultZoneSet _PrintDebugInfo
  48.  
  49.  
  50. class DW_EXPORT DwDateTime : public DwFieldBody {
  51.  
  52. public:
  53.  
  54.     DwDateTime();
  55.     DwDateTime(const DwDateTime& aDateTime);
  56.     DwDateTime(const DwString& aStr, DwMessageComponent* aParent=0);
  57.     //. The first constructor is the default constructor, which assigns
  58.     //. the current date and time as reported by the operating system.
  59.     //.
  60.     //. The second constructor is the copy constructor.  The parent of
  61.     //. the new {\tt DwDateTime} object is set to {\tt NULL}.
  62.     //.
  63.     //. The third constructor sets {\tt aStr} as the {\tt DwDateTime}
  64.     //. object's string representation and sets {\tt aParent} as its parent.
  65.     //. The virtual member function {\tt Parse()} should be called after
  66.     //. this constructor to extract the date and time information from the
  67.     //. string representation.  Unless it is {\tt NULL}, {\tt aParent} should
  68.     //. point to an object of a class derived from {\tt DwField}.
  69.  
  70.     virtual ~DwDateTime();
  71.  
  72.     const DwDateTime& operator = (const DwDateTime& aDateTime);
  73.     //. This is the assignment operator, which sets this {\tt DwDateTime}
  74.     //. object to the same value as {\tt aDateTime}.
  75.  
  76.     virtual void Parse();
  77.     //. This virtual function, inherited from {\tt DwMessageComponent},
  78.     //. executes the parse method for {\tt DwDateTime} objects. The parse
  79.     //. method creates or updates the broken-down representation from the
  80.     //. string representation.  For {\tt DwDateTime} objects, the parse
  81.     //. method parses the string representation to extract the year,
  82.     //. month, day, hour, minute, second, and time zone.
  83.     //.
  84.     //. This function clears the is-modified flag.
  85.  
  86.     virtual void Assemble();
  87.     //. This virtual function, inherited from {\tt DwMessageComponent},
  88.     //. executes the assemble method for {\tt DwDateTime} objects.
  89.     //. It should be called whenever one of the object's attributes
  90.     //. is changed in order to assemble the string representation from
  91.     //. its broken-down representation.  It will be called
  92.     //. automatically for this object by the parent object's
  93.     //. {\tt Assemble()} member function if the is-modified flag is set.
  94.     //.
  95.     //. This function clears the is-modified flag.
  96.  
  97.     virtual DwMessageComponent* Clone() const;
  98.     //. This virtual function, inherited from {\tt DwMessageComponent},
  99.     //. creates a new {\tt DwDateTime} on the free store that has the same
  100.     //. value as this {\tt DwDateTime} object.  The basic idea is that of
  101.     //. a virtual copy constructor.
  102.  
  103.     DwUint32 AsUnixTime() const;
  104.     //. Returns the date and time as a UNIX (POSIX) time, defined as the
  105.     //. number of seconds elapsed since 1 Jan 1970 00:00:00 UTC.
  106.  
  107.     void FromUnixTime(DwUint32 aTime);
  108.     //. Sets the date and time from {\tt aTime}, interpreted as the number of
  109.     //. of seconds elapsed since 1 Jan 1970 00:00:00 UTC.
  110.  
  111.     void FromCalendarTime(time_t aTime);
  112.     //. Sets the date and time from {\tt aTime}, which is assumed to be in a
  113.     //. format compatible with the native {\tt time()} ANSI C function.
  114.     //. For most UNIX systems, this function is the same as the function
  115.     //. {\tt FromUnixTime()}.  (For efficiency, use {\tt FromUnixTime()}
  116.     //. instead of {\tt FromCalendarTime()} if possible).
  117.  
  118.     DwInt32 DateAsJulianDayNum() const;
  119.     //. Returns the Julian Day Number, defined as the number of days elapsed
  120.     //. since 1 Jan 4713 BC.  The JDN is calculated directly from the values
  121.     //. of the year, month, and day; time zone information is ignored.
  122.  
  123.     void DateFromJulianDayNum(DwInt32 aJdn);
  124.     //. Sets the year, month, and day from {\tt aJdn}, interpreted as a Julian
  125.     //. Day Number.  By definition, the JDN is the number of days elapsed
  126.     //. since 1 Jan 4713 BC.  This member function ignores time zone
  127.     //. information.
  128.  
  129.     DwInt32 TimeAsSecsPastMidnight() const;
  130.     //. Returns the number of seconds past midnight.  The value is
  131.     //. calculated directly from the values of the hour, minute, and
  132.     //. second; time zone information is ignored.
  133.  
  134.     void TimeFromSecsPastMidnight(DwInt32 aSecs);
  135.     //. Sets the hour, minute, and second from {\tt aSecs}, interpreted as the
  136.     //. number of seconds elapsed since midnight.  This member function
  137.     //. ignores time zone information.  The argument {\tt aSecs} should be in
  138.     //. the range 0 to 86399, inclusive.
  139.  
  140.     int Year() const;
  141.     //. Returns the four digit year, e.g. 1997.
  142.  
  143.     void SetYear(int aYear);
  144.     //. Sets the year from {\tt aYear}, which should be a four digit year.
  145.  
  146.     int Month() const;
  147.     //. Returns the month.  Values range from 1 to 12.
  148.  
  149.     void SetMonth(int aMonth);
  150.     //. Sets the month from {\tt aMonth}, which should be in the range 1
  151.     //. to 12.
  152.  
  153.     int Day() const;
  154.     //. Returns the day of the month.  Values range from 1 to 31.
  155.  
  156.     void SetDay(int aDay);
  157.     //. Sets the day of the month from {\tt aDay}.
  158.  
  159.     int Hour() const;
  160.     //. Returns the hour according to the 24 hour clock.
  161.     //. Values range from 0 to 23.
  162.  
  163.     void SetHour(int aHour);
  164.     //. Sets the hour from {\tt aHour} based on the 24-hour clock. {\tt aHour}
  165.     //. should be in the range 0 to 23.
  166.  
  167.     int Minute() const;
  168.     //. Returns the minute.  Values range from 0 to 59.
  169.  
  170.     void SetMinute(int aMinute);
  171.     //. Sets the minute from {\tt aMinute}, which should be in the range 0
  172.     //. to 59.
  173.  
  174.     int Second() const;
  175.     //. Returns the second.  Values range from 0 to 59.
  176.  
  177.     void SetSecond(int aSecond);
  178.     //. Sets the second from {\tt aSecond}, which should be in the range 0
  179.     //. to 59.
  180.  
  181.     int Zone() const;
  182.     //. Returns the time zone as the diffence in minutes between local time
  183.     //. and Coordinated Universal Time (UTC or GMT).
  184.  
  185.     void SetZone(int aZone);
  186.     //. Sets the time zone from {\tt aZone}, interpreted as the time difference
  187.     //. in minutes between local time and Coordinated Universal Time
  188.     //. (UTC, or GMT).
  189.  
  190.     static void SetDefaultZone(int aZone);
  191.     //. Sets the default time zone.  {\tt aZone} should be the time difference
  192.     //. in minutes between local time and Coordinated Universal Time
  193.     //. (UTC, or GMT).  The value is used to set the time zone for any
  194.     //. objects created using the default constructor.
  195.  
  196.     static DwDateTime* NewDateTime(const DwString&, DwMessageComponent*);
  197.     //. Creates a new {\tt DwDateTime} object on the free store.
  198.     //. If the static data member {\tt sNewDateTime} is {\tt NULL},
  199.     //. this member function will create a new {\tt DwDateTime}
  200.     //. and return it.  Otherwise, {\tt NewDateTime()} will call
  201.     //. the user-supplied function pointed to by {\tt sNewDateTime},
  202.     //. which is assumed to return an object from a class derived from
  203.     //. {\tt DwDateTime}, and return that object.
  204.  
  205.     //+ Var sNewDateTime
  206.     static DwDateTime* (*sNewDateTime)(const DwString&, DwMessageComponent*);
  207.     //. If {\tt sNewDateTime} is not {\tt NULL}, it is assumed to point to a
  208.     //. user-supplied function that returns an object from a class derived
  209.     //. from {\tt DwDateTime}.
  210.  
  211. protected:
  212.  
  213.     void _FromUnixTime(DwUint32 aTime);
  214.     //. Like {\tt FromUnixTime()}, but doesn't set the is-modified flag.
  215.  
  216.     void _FromCalendarTime(time_t aTime);
  217.     //. Like {\tt FromCalendarTime()}, but doesn't set the is-modified flag.
  218.  
  219.     int  mYear;
  220.     int  mMonth;
  221.     int  mDay;
  222.     int  mHour;
  223.     int  mMinute;
  224.     int  mSecond;
  225.     int  mZone;
  226.  
  227.     static int sDefaultZone;
  228.     static int sIsDefaultZoneSet;
  229.  
  230. private:
  231.  
  232.     static const char* const sClassName;
  233.  
  234.     void Init();
  235.     //. Initialization code common to all constructors.
  236.  
  237. public:
  238.  
  239.     virtual void PrintDebugInfo(std::ostream& aStrm, int aDepth=0) const;
  240.     //. This virtual function, inherited from {\tt DwMessageComponent},
  241.     //. prints debugging information about this object to {\tt aStrm}.
  242.     //.
  243.     //. This member function is available only in the debug version of
  244.     //. the library.
  245.  
  246.     virtual void CheckInvariants() const;
  247.     //. Aborts if one of the invariants of the object fails.  Use this
  248.     //. member function to track down bugs.
  249.     //.
  250.     //. This member function is available only in the debug version of
  251.     //. the library.
  252.  
  253. protected:
  254.  
  255.     void _PrintDebugInfo(std::ostream& aStrm) const;
  256.  
  257. };
  258.  
  259.  
  260. inline int DwDateTime::Year() const
  261. {
  262.     return mYear;
  263. }
  264.  
  265.  
  266. inline int DwDateTime::Month() const
  267. {
  268.     return mMonth;
  269. }
  270.  
  271.  
  272. inline int DwDateTime::Day() const
  273. {
  274.     return mDay;
  275. }
  276.  
  277.  
  278. inline int DwDateTime::Hour() const
  279. {
  280.     return mHour;
  281. }
  282.  
  283.  
  284. inline int DwDateTime::Minute() const
  285. {
  286.     return mMinute;
  287. }
  288.  
  289.  
  290. inline int DwDateTime::Second() const
  291. {
  292.     return mSecond;
  293. }
  294.  
  295.  
  296. inline int DwDateTime::Zone() const
  297. {
  298.     return mZone;
  299. }
  300.  
  301.  
  302. inline void DwDateTime::SetYear(int aYear)
  303. {
  304.     mYear = aYear;
  305.     SetModified();
  306. }
  307.  
  308.  
  309. inline void DwDateTime::SetMonth(int aMonth)
  310. {
  311.     mMonth = aMonth;
  312.     SetModified();
  313. }
  314.  
  315.  
  316. inline void DwDateTime::SetDay(int aDay)
  317. {
  318.     mDay = aDay;
  319.     SetModified();
  320. }
  321.  
  322.  
  323. inline void DwDateTime::SetHour(int aHour)
  324. {
  325.     mHour = aHour;
  326.     SetModified();
  327. }
  328.  
  329.  
  330. inline void DwDateTime::SetMinute(int aMinute)
  331. {
  332.     mMinute = aMinute;
  333.     SetModified();
  334. }
  335.  
  336.  
  337. inline void DwDateTime::SetSecond(int aSecond)
  338. {
  339.     mSecond = aSecond;
  340.     SetModified();
  341. }
  342.  
  343.  
  344. inline void DwDateTime::SetZone(int aZone)
  345. {
  346.     mZone = aZone;
  347.     SetModified();
  348. }
  349.  
  350. #endif
  351.